package com.czwx.bee.business.manager.modular.statistics.service.impl;

import cn.hutool.core.date.DateUtil;
import com.czwx.bee.business.manager.modular.main.dto.DayLoanMoneyDto;
import com.czwx.bee.business.manager.modular.main.dto.TodayRemindDto;
import com.czwx.bee.business.manager.modular.statistics.service.MainIndexService;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:wiki
 * @Date:Create in 2018/7/11 0011 下午 12:00
 */
@Service
public class MainIndexServiceImpl implements MainIndexService {
    @Autowired
    SQLManager sqlManager;

    @Override
    public TodayRemindDto todayRemind(Map params) {
        String todayStr=DateUtil.format(new Date(),"yyyy-MM-dd");
        Date today=DateUtil.parse(todayStr);
        Date yesterday=DateUtil.offsetDay(today,-1);
        Date threeDaysAgo=DateUtil.offsetDay(today,-3);
        params.put("today",today);
        params.put("yesterday",yesterday);
        params.put("threeDaysAgo",threeDaysAgo);

        return sqlManager.selectSingle("statistics.Statistics.todayRemind",params,TodayRemindDto.class);
    }

    @Override
    public List<DayLoanMoneyDto> loanMoneyDay(Map params) {
        String todayStr=DateUtil.format(new Date(),"yyyy-MM-dd");
        Date endTime =DateUtil.parse(todayStr);
        Date startTime=DateUtil.offsetDay(endTime,-14);

        String sql="SELECT a.date,(SELECT count(c.id) as data FROM cl_user b JOIN cl_borrow c ON c.user_id = b.id JOIN cl_borrow_progress d ON c.id = d.borrow_id " +
                "WHERE " +
                "DATE_FORMAT(c.create_time, '%y-%m-%d') = DATE_FORMAT(a.date, '%y-%m-%d') and b.drainage_id=#drainageId#) as data FROM bp_date a " +
                "WHERE " +
                "a.date >= #startTime# AND a.date <=#endTime# ";
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        List<DayLoanMoneyDto> list=sqlManager.execute(sql,DayLoanMoneyDto.class,params);
        return list;
    }

    @Override
    public String getShopUrl(Map params) {
        String sql="select (SELECT value from arc_sys_config where code='h5_platform_reg_url')as url,platform from cl_drainage_platform where id=#drainageId#";
        List<Map> list=sqlManager.execute(sql,Map.class,params);
        if(list.size()==0){
            return null;
        }else {
            Map map=list.get(0);
            String url= (String) map.get("url")+"?platform="+(String) map.get("platform");
            return url;
        }
    }
}
